" Plug:" {{{
let s:current_file_path = expand('<sfile>')
let s:vimfiles = fnamemodify(s:current_file_path, ':p:h:h:h')

function! s:add_to_path(path) abort
    let rtps = split(&rtp, ',')
    let index = match(rtps, a:path)
    if index < 0
        call add(rtps, a:path)
        echo 'rtps = ' .. join(rtps, "\n")
    endif
    let &rtp = join(rtps, ',')
endfunction

function! api#plug#init() abort
    let vimfiles = get(g:, 'vimfiles', s:vimfiles)
    let plug_plugin_root = expand(vimfiles . '/pack/github.com/start/vim-plug')
    let plug_vim_file = glob(plug_plugin_root .. '/plug.vim')
    if filereadable(plug_vim_file)
        execute 'source ' .. plug_vim_file
        call assert_true(g:loaded_plug)
    endif
    if exists('g:loaded_plug')
        nnoremap <leader>i :<c-u>call api#plug#start()<cr>
        nnoremap <leader>I :<c-u>call api#plug#opt()<cr>
    endif
endfunction
" }}}

let g:github_proxy_prefix = 'https://ghfast.top/'
let g:github_prefix = g:github_proxy_prefix .. 'https://github.com/'
let g:gitee_prefix = 'https://gitee.com/BIT_chenxinliang/'
function! api#plug#start() abort
    let vimfiles = get(g:, 'vimfiles', s:vimfiles)
    let pack_plugin_start = expand(vimfiles . '/pack/github.com/start')
    let prefix = g:github_prefix
    call plug#begin(pack_plugin_start)
    Plug prefix .. 'dense-analysis/ale'
    Plug prefix .. 'kien/ctrlp.vim'
    Plug prefix .. 'morhetz/gruvbox'
    Plug prefix .. 'SirVer/ultisnips' 
    Plug prefix .. 'Shougo/unite.vim'
    Plug prefix .. 'yianwillis/vimcdoc'
    Plug prefix .. 'Shougo/vimproc'
    Plug prefix .. 'Shougo/vimfiler'
    Plug prefix .. 'vim-airline/vim-airline'
    Plug prefix .. 'vim-airline/vim-airline-themes'
    Plug prefix .. 'tpope/vim-fugitive'
    Plug prefix .. 'bling/vim-bufferline'
    Plug prefix .. 'junegunn/vim-plug'
    Plug prefix .. 'junegunn/vim-easy-align'
    Plug prefix .. 'junegunn/fzf'
    Plug prefix .. 'junegunn/fzf.vim'
    Plug prefix .. 'mhinz/vim-startify'
    call plug#end()
    PlugInstall
endfunction

function! api#plug#opt() abort
    let vimfiles = get(g:, 'vimfiles', s:vimfiles)
    let pack_plugin_opt = expand(vimfiles . '/pack/github.com/opt')
    call plug#begin(pack_plugin_opt)
    let prefix = g:github_prefix
    Plug prefix .. 'dense-analysis/ale'
    Plug prefix .. 'prabirshrestha/async.vim'
    Plug prefix .. 'prabirshrestha/asyncomplete.vim'
    Plug prefix .. 'prabirshrestha/asyncomplete-lsp.vim'
    Plug prefix .. 'prabirshrestha/asyncomplete-file.vim'
    Plug prefix .. 'prabirshrestha/asyncomplete-tags.vim'
    Plug prefix .. 'prabirshrestha/asyncomplete-ultisnips.vim'
    " Plug 'prabirshrestha/asyncomplete-clang.vim'
    Plug prefix .. 'andreypopp/asyncomplete-ale.vim'
    " Plug 'laixintao/asyncomplete-gitcommit.vim'
    Plug prefix .. 'skywind3000/asyncrun.vim'
    Plug prefix .. 'skywind3000/asynctasks.vim'
    Plug prefix .. 'skywind3000/vim-quickui'
    Plug prefix .. 'skywind3000/vim-terminal-help'
    Plug prefix .. 'sheerun/vim-polyglot'
    Plug prefix .. 'jiangmiao/auto-pairs'
    Plug prefix .. 'vim-scripts/AutoComplPop'
    Plug prefix .. 'jlanzarotta/bufexplorer'
    Plug prefix .. 'github/copilot.vim'
    Plug prefix .. 'kien/ctrlp.vim'
    Plug prefix .. 'xqin/gvimfullscreen'
    Plug prefix .. 'iamcco/markdown-preview.vim'
    Plug prefix .. 'iamcco/mathjax-support-for-mkdp'
    Plug prefix .. 'mracos/mermaid.vim'
    " Plug prefix .. 'gergap/vim-ollama'
    Plug prefix .. 'python-mode/python-mode', { 'for': 'python', 'branch': 'develop' }
    Plug prefix .. 'preservim/tagbar'
    Plug prefix .. 'tc50cal/vim-terminal'
    Plug prefix .. 'tracyone/term.vim'
    Plug prefix .. 'voldikss/vim-floaterm'
    Plug prefix .. 'gagbo/vim-gnuplot'
    Plug prefix .. 'iamcco/vim-language-server'
    Plug prefix .. 'craigmac/vim-mermaid'
    Plug prefix .. 'vim-scripts/mru.vim'
    Plug prefix .. 'Yggdroot/LeaderF'
    Plug prefix .. 'voldikss/LeaderF-everything'
    Plug prefix .. 'linjiX/LeaderF-git'
    Plug prefix .. 'neomake/neomake'
    Plug prefix .. 'preservim/nerdtree', { 'on': 'NERDTreeToggle' }
    Plug prefix .. 'SirVer/ultisnips' 
    Plug prefix .. 'Shougo/unite.vim'
    Plug prefix .. 'Shougo/unite-outline'
    Plug prefix .. 'bling/vim-bufferline'
    Plug prefix .. 'easymotion/vim-easymotion'
    Plug prefix .. 'ludovicchabant/vim-gutentags'
    Plug prefix .. 'puremourning/vimspector'
    Plug prefix .. 'iamcco/vim-language-server'
    Plug prefix .. 'prabirshrestha/vim-lsp'
    Plug prefix .. 'mattn/vim-lsp-settings'
    Plug prefix .. 'abbiekressner/vim-matlab'
    Plug prefix .. 'vim-pandoc/vim-pandoc'
    Plug prefix .. 'vim-pandoc/vim-pandoc-syntax'
    Plug prefix .. 'dbakker/vim-projectroot'
    Plug prefix .. 'tpope/vim-commentary'
    Plug prefix .. 'tpope/vim-dispatch'
    Plug prefix .. 'tpope/vim-fugitive'
    Plug prefix .. 'tpope/vim-sensible'
    Plug prefix .. 'tpope/vim-unimpaired'
    Plug prefix .. 'honza/vim-snippets'
    Plug prefix .. 'lukelbd/vim-tabline'
    Plug prefix .. 'tibabit/vim-templates'
    " Plug prefix .. 'kana/vim-textobj-comment'
    Plug prefix .. 'kana/vim-textobj-entire'
    Plug prefix .. 'kana/vim-textobj-user'
    Plug prefix .. 'kana/vim-textobj-function'
    Plug prefix .. 'kana/vim-textobj-line'
    Plug prefix .. 'kana/vim-textobj-indent'
    " Plug prefix .. 'kana/vim-textobj-parameter'
    Plug prefix .. 'kana/vim-textobj-syntax'
    Plug prefix .. 'kana/vim-textobj-fold'
    Plug prefix .. 'kana/vim-textobj-lastpat'
    Plug prefix .. 'libclang-vim/vim-textobj-clang'
    Plug prefix .. 'rbonvall/vim-textobj-latex'
    Plug prefix .. 'preservim/vim-textobj-sentence'
    Plug prefix .. 'jceb/vim-textobj-uri'
    Plug prefix .. 'liuchengxu/vim-which-key'
    Plug prefix .. 'yianwillis/vimcdoc'
    Plug prefix .. 'Shougo/vimfiler'
    Plug prefix .. 'Shougo/vimproc'
    Plug prefix .. 'lervag/vimtex'
    Plug prefix .. 'vimwiki/vimwiki'
    " Plug 'vim-scripts/VimIM'
    " Plug 'ziglang/zig.vim'
    " " Plug 'ivanov/vim-ipython'
    " Plug 'jupyter-vim/jupyter-vim'
    " " Plug 'Valloric/YouCompleteMe'
    " Plug 'leafOfTree/vim-project'
    " Plug 'lv99/MultiProject.vim'
    " Plug 'JeroenMulkers/matvim'
    " Plug 'jpalardy/vim-slime'
    " Plug 'MattesGroeger/vim-bookmarks'
    " " Plug 'neoclide/coc.nvim', {'branch': 'master', 'do': 'yarn install --frozen-lockfile'}
    " " Plug 'neoclide/coc-tsserver', {'do': 'yarn install --frozen-lockfile'}
    " Plug 'vimoutliner/vimoutliner'
    " Plug 'vim-scripts/NrrwRgn'
    call plug#end()
    PlugInstall
endfunction

function! s:get_repo_url() abort
    let text = system('git remote -v')
    let lines = split(text, '\n')
    if len(lines) > 0
        let line = lines[0]
        let tokens = split(line)
        return tokens[1]
    endif
endfunction

function! s:get_new_url(url) abort
    return substitute(a:url, 
                \     '^https:\/\/\(git::@\)\?github.com',
                \     g:github_proxy_prefix .. 'https://github.com',
                \     v:true
                \    )
endfunction

function! s:update_repo(repo) abort
    let cwd = chdir(a:repo)
    let url = s:get_repo_url()
    let index = match(url, '^https://git::@github.com')
    if index == 0
        let new_url = s:get_new_url(url)
        let cmd = 'git remote add proxy ' .. new_url
        let output = system(cmd)
        if !empty(output)
            echoerr output
        endif
    endif
    let cmd = 'git pull proxy '
    let output = system(cmd)
    if !empty(output)
        echoerr output
    endif
    call chdir(cwd)
endfunction

function! api#plug#update() abort
    let vimfiles = get(g:, 'vimfiles', s:vimfiles)
    let pack_plugin_start = expand(vimfiles . '/pack/github.com/start')
    let pack_plugin_opt = expand(vimfiles . '/pack/github.com/opt')
    let repos = globpath(pack_plugin_opt, '*', v:true, v:true)
    for repo in repos
        call s:update_repo(repo)
    endfor
endfunction

